package com.apollo.demo.controller;

import com.apollo.demo.dao.StudentMapper;
import com.apollo.demo.dao.PlusMapper;
import com.apollo.demo.exception.*;
import com.apollo.demo.model.*;
import com.apollo.demo.model.StudentPlus;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生表接口类
 */
@Api(value = "学生表", tags = "学生表接口模块")
@RestController
@RequestMapping("/student")
@ResponseBody
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private PlusMapper plusMapper;

    /**
     * 根据学生ID获取学生基本信息
     */
    @ApiOperation(value = "查询学生信息", notes = "根据学生id查询学生信息")
    @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public Student getStudentById(@PathVariable(name = "id") String id) throws Exception {
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        criteria.andSidEqualTo(id);
        List<Student> students = studentMapper.selectByExample(example);
        if (students != null && students.size() > 0) {
            return students.get(0);
        }
        throw new ItemNotFoundException(id);
    }

    /**
     * 根据学生ID获取学生考试成绩
     */
    @ApiOperation(value = "查询学生考试成绩", notes = "根据学生id查询学生考试成绩")
    @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "getScores/{id}")
    public StudentPlus getScoresById(@PathVariable(name = "id") String id) throws Exception {
        List<StudentPlus> studentPluses = plusMapper.getById(id);
        if (studentPluses != null && studentPluses.size() > 0) {
            return studentPluses.get(0);
        }
        throw new ItemNotFoundException(id);
    }

}